package com.wephone.Student.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.ShareReferenceUtil;

public class CreateGroupActivity extends BaseActivity {
	private EditText inputEdit1;
	private EditText inputEdit2;
	private EditText inputEdit3;
	private EditText inputEdit4;
	
	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(CreateGroupActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
//					FlyTogetherListResponse res = (FlyTogetherListResponse)response;
					setResult(222);
					finish();
				}
			}
		};
		return handler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_group);
		
		init();
	}
	
	private void init(){
		inputEdit1 = (EditText)findViewById(R.id.group_input_edit_1);
		inputEdit2 = (EditText)findViewById(R.id.group_input_edit_2);
		inputEdit3 = (EditText)findViewById(R.id.group_input_edit_3);
		inputEdit4 = (EditText)findViewById(R.id.group_input_edit_4);
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		findViewById(R.id.btn_finish).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(checkInput()){
					RequestParams params = new RequestParams();
					params.put("gLeaderId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
					params.put("gCountry", inputEdit1.getText().toString());
					params.put("gCity", inputEdit2.getText().toString());
					params.put("gTitle", inputEdit3.getText().toString());
					params.put("gDetail", inputEdit4.getText().toString());
					params.put("gGpsX", ShareReferenceUtil.getLocationLatitude(getApplicationContext()));
					params.put("gGpsY", ShareReferenceUtil.getLocationLontitude(getApplicationContext()));
					HttpClient.getInstance().get(Constants.API_ADD_GROUP_URL, params, getHandler());
				}
			}
		});
	}

	private boolean checkInput(){
		if(TextUtils.isEmpty(inputEdit1.getText().toString())){
			Toast.makeText(getApplicationContext(), "所在国家必须填写", Toast.LENGTH_SHORT).show();
			return false;
		}else if(TextUtils.isEmpty(inputEdit2.getText().toString())){
			Toast.makeText(getApplicationContext(), "所在城市必须填写", Toast.LENGTH_SHORT).show();
			return false;
		}else if(TextUtils.isEmpty(inputEdit3.getText().toString())){
			Toast.makeText(getApplicationContext(), "群组主题必须填写", Toast.LENGTH_SHORT).show();
			return false;
		}else if(TextUtils.isEmpty(inputEdit4.getText().toString())){
			Toast.makeText(getApplicationContext(), "群组介绍必须填写", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
	
}
